/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.yandex.bank;

import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 * @author Neophron
 */
public class Account {

    private final AtomicInteger amount;

    Account(int amount) {
        this.amount = new AtomicInteger(amount);
    }

    public int get() {
        return amount.get();
    }

    public void deposit(int amount) {
        this.amount.addAndGet(amount);
    }

    public int withdraw(int amount) {
        int res = 0;
        for (;;) {
            res = 0;
            int oldAm = this.amount.get();
            if (oldAm >= amount) {
                int newAm = oldAm - amount;
                res = amount;
                if (this.amount.compareAndSet(oldAm, newAm)) {
                    break;
                }
            } else {
                break;
            }
        }
        return res;
    }

}
